package com.utils;

import io.netty.util.Timeout;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author ben.huang
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 存储数据
     * @param key
     * @param value
     * @param timeout
     */
    public void set(String key,String value,long timeout){
        redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }

    /**
     * 根据Key值获取结果
     * @param key
     * @return
     */
    public String get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除指定的key值
     * @param key
     */
    public void delete(String key){
        redisTemplate.delete(key);
    }

    /**
     * 判断指定的key是否已过期
     * @param key
     * @param timeout
     * @param timeUnit
     * @return
     */
    public boolean expire(String key, long timeout, TimeUnit timeUnit){
        return Boolean.TRUE.equals(redisTemplate.expire(key,timeout,timeUnit));
    }

    /**
     * 判断是否含有指定的Key
     * @param key
     * @return
     */
    public boolean haskey(String key){
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }


}
